package com.web.exiaodao.business.service.impl;

import com.web.exiaodao.business.entity.InsRoomsOrder;
import com.web.exiaodao.business.repository.InsRoomsOrderRepository;
import com.web.exiaodao.business.service.InsRoomsOrderService;
import com.web.webstart.base.constant.XaConstant;
import com.web.webstart.base.exception.BusinessException;
import com.web.webstart.base.service.impl.BaseService;
import com.web.webstart.base.util.DateUtils;
import com.web.webstart.base.util.XaUtil;
import com.web.webstart.base.util.jpa.DynamicSpecifications;
import com.web.webstart.base.util.jpa.SearchFilter;
import com.web.webstart.base.util.jpa.SearchFilter.Operator;
import com.web.webstart.base.util.result.XaResult;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service("InsRoomsOrderService")
@Transactional(readOnly = false)
public class InsRoomsOrderServiceImpl extends BaseService<InsRoomsOrder> implements InsRoomsOrderService {

	private static final Logger LOGGER = Logger.getLogger(InsRoomsOrderServiceImpl.class);

	@Autowired
	private InsRoomsOrderRepository insRoomsOrderRepository;
	
	/**
	 * 查询单条InsRoomsOrder信息
	 * //@param tId
	 * @return 返回单个InsRoomsOrder对象
	 * @throws BusinessException
	 */
	@Transactional(readOnly = true, rollbackFor = Exception.class)
	public XaResult<InsRoomsOrder> findOne(Long modelId) throws BusinessException {
		InsRoomsOrder obj = new InsRoomsOrder();
		if(modelId != 0){
			obj = insRoomsOrderRepository.findByIdAndStatusNot(modelId,XaConstant.Status.delete);
		}
		XaResult<InsRoomsOrder> xr = new XaResult<InsRoomsOrder>();
		if (XaUtil.isNotEmpty(obj)) {
			xr.setObject(obj);
			LOGGER.info("后台----->查询单个实体----->InsRoomsOrder");
		} else {
			xr.error(XaConstant.Message.object_not_find);
			LOGGER.info(XaConstant.Message.object_not_find);
		}
		return xr;
	}

	/**
	 * 分页查询状态非status的InsRoomsOrder数据
	 * @param status
	 * @param filterParams
	 * @param pageable
	 * @return 返回对象InsRoomsOrder集合
	 * @throws BusinessException
	 */
	@Transactional(readOnly = true, rollbackFor = Exception.class)
	public XaResult<Page<InsRoomsOrder>> findListNEStatusByFilter(
			Integer status, Map<String, Object> filterParams, Pageable pageable) throws BusinessException {
		Map<String, SearchFilter> filters = SearchFilter.parse(filterParams);
		if(status == null){// 默认显示非删除的所有数据
			status = XaConstant.Status.delete;
		}
		filters.put("status", new SearchFilter("status", Operator.NE, status));
		Page<InsRoomsOrder> page = insRoomsOrderRepository.findAll(DynamicSpecifications
				.bySearchFilter(filters.values(), InsRoomsOrder.class), pageable);
		XaResult<Page<InsRoomsOrder>> xr = new XaResult<Page<InsRoomsOrder>>();
		xr.setObject(page);
		LOGGER.info("后台----->分页查询实体集合（状态不等于）----->InsRoomsOrder");
		return xr;
	}

	/**
	 * 分页查询状态status的InsRoomsOrder数据
	 * @param status
	 * @param filterParams
	 * @param pageable
	 * @return 返回对象InsRoomsOrder集合
	 * @throws BusinessException
	 */
	@Transactional(readOnly = true, rollbackFor = Exception.class)
	public XaResult<Page<InsRoomsOrder>> findListEQStatusByFilter(
			Integer status, Map<String, Object> filterParams, Pageable pageable) throws BusinessException {
		Map<String, SearchFilter> filters = SearchFilter.parse(filterParams);
		if(status == null){// 默认显示正常数据
			status = XaConstant.Status.valid;
		}
		filters.put("status", new SearchFilter("status", Operator.EQ, status));
		Page<InsRoomsOrder> page = insRoomsOrderRepository.findAll(DynamicSpecifications
				.bySearchFilter(filters.values(), InsRoomsOrder.class), pageable);
		XaResult<Page<InsRoomsOrder>> xr = new XaResult<Page<InsRoomsOrder>>();
		xr.setObject(page);
		LOGGER.info("后台----->分页查询实体集合（状态等于）----->InsRoomsOrder");
		return xr;
	}

	/**
	 * 保存InsRoomsOrder信息
	 * @param model
	 * @return
	 * @throws BusinessException
	 */
	@Transactional(rollbackFor = Exception.class)
	public XaResult<InsRoomsOrder> saveOrUpdate(InsRoomsOrder model) throws BusinessException {
		InsRoomsOrder obj = null;
		if(XaUtil.isNotEmpty(model.getId())){
			obj = insRoomsOrderRepository.findOne(model.getId());
		}else{
			obj = new InsRoomsOrder();
		}
	obj.setCreateUser(XaUtil.isNotEmpty(model.getCreateUser()) ? model.getCreateUser() : obj.getCreateUser());
	    obj.setInsRoomId(XaUtil.isNotEmpty(model.getInsRoomId()) ? model.getInsRoomId() : obj.getInsRoomId());
	    obj.setDate(XaUtil.isNotEmpty(model.getDate()) ? model.getDate() : obj.getDate());
	    obj.setRoomNo(XaUtil.isNotEmpty(model.getRoomNo()) ? model.getRoomNo() : obj.getRoomNo());
	    obj.setRoomOrderState(XaUtil.isNotEmpty(model.getRoomOrderState()) ? model.getRoomOrderState() : obj.getRoomOrderState());
		obj = insRoomsOrderRepository.save(obj);
		XaResult<InsRoomsOrder> xr = new XaResult<InsRoomsOrder>();
		xr.setObject(obj);
		LOGGER.info("后台新增或编辑数据成功！");
		return xr;
	}

	/**
	 * 修改InsRoomsOrder状态，可一次修改多条   3删除  -1锁定  1正常
	 * //@param userId
	 * @param modelIds
	 * @param status
	 * @return 返回InsRoomsOrder对象
	 * @throws BusinessException
	 */
	@Transactional(rollbackFor = Exception.class)
	public XaResult<InsRoomsOrder> multiOperate(
			String modelIds,Integer status) throws BusinessException {
		XaResult<InsRoomsOrder> xr = new XaResult<InsRoomsOrder>();
		if(status == null){
			status = XaConstant.Status.delete;
		}
		if(modelIds != null){
			String[] ids = modelIds.split(",");
			for(String id : ids){
				InsRoomsOrder obj = insRoomsOrderRepository.findByIdAndStatusNot(Long.parseLong(id),status);
				if (XaUtil.isNotEmpty(obj)) {
					obj.setStatus(status);
					obj = insRoomsOrderRepository.save(obj);
				}
			}
		}
		LOGGER.info("批量修改数据状态成功！");
		return xr;
	}

	/**
	 * 生成酒店的订单信息 按天
	 * @param insRoomId  酒店房型id
	 * @param days       天数;今天以后的n天；
	 * @return
	 * @throws BusinessException
	 */
	@Override
	public XaResult<List<InsRoomsOrder>> generateDateByDay(long insRoomId, int days) throws BusinessException {
		List<InsRoomsOrder> orders = new ArrayList<>();
		for (int i = 0; i < days; ++i) {
			String today = DateUtils.getStrDate(DateUtils.addDay(new Date(), i), DateUtils.YYYYMMDD);
			InsRoomsOrder order = new InsRoomsOrder(insRoomId, today, i+1l, new Integer(0));
			orders.add(order);
		}
		Iterable<InsRoomsOrder> iterable = insRoomsOrderRepository.save(orders);
		List<InsRoomsOrder> insRoomsOrders = com.google.common.collect.Lists.newArrayList(iterable);
		return new XaResult<>(insRoomsOrders);
	}

	/**
	 * 生成酒店的订单信息  按月
	 * @param insRoomId  酒店房型id
	 * @param months       月数
	 * @return
	 * @throws BusinessException
	 */
	@Override
	public XaResult<List<InsRoomsOrder>> generateDateByMonth(long insRoomId, int months) throws BusinessException {
		List<InsRoomsOrder> orders = new ArrayList<>();
		for (int i = 0; i < months; ++i) {
			String today = DateUtils.getStrDate(DateUtils.addDay(new Date(), i), DateUtils.YYYYMMDD);
			InsRoomsOrder order = new InsRoomsOrder(insRoomId, today, i+1l, new Integer(0));
			orders.add(order);
		}
		Iterable<InsRoomsOrder> iterable = insRoomsOrderRepository.save(orders);
		List<InsRoomsOrder> insRoomsOrders = com.google.common.collect.Lists.newArrayList(iterable);
		XaResult<List<InsRoomsOrder>> xr = new XaResult<>(insRoomsOrders);
		return xr;
	}
}
